package com.example.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.base.entity.Skill;
import com.example.base.entity.Type;
import com.example.base.service.TypeService;
import com.example.base.util.Response;
import com.example.base.util.validator.CourseCtrlValidate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    TypeService typeService;

    @GetMapping("/get")
    public Response getType(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "size",required = false) Integer size,
            Type type
    ){
        if (ObjectUtils.isEmpty(page)) {
            page = 1;
        }
        if (ObjectUtils.isEmpty(size)) {
            size = 5;
        }

        IPage<Type> typeIPage = typeService.selectType(type, page, size);
        typeIPage.setTotal(typeIPage.getTotal());

        Response response = new Response();
        response.setData(typeIPage);
        response.setMsg("类型查询成功");
        response.setStatus(200);

        return response;
    }

    @GetMapping("/getAll")
    public Response getAllSkill(){

        List<Type> list = typeService.selectAllType();

        Response response = new Response();
        response.setData(list);
        response.setMsg("所有类型查询成功");
        response.setStatus(200);

        return response;
    }

    @PostMapping("/add")
    public Response addType(@RequestBody Type type){

        // 生成添加时间
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = simpleDateFormat.format(now);

        type.setCreateTime(createTime);

        Response response = new Response();

        int statusNum = typeService.addType(type);

        if (statusNum == 1) {
            response.setStatus(200);
            response.setData(statusNum);
            response.setMsg("添加类型成功");
        }
        return response;
    }

    @PutMapping("/update")
    public Response updateType(@RequestBody Type type){

        // 生成添加修改时间
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String updateTime = simpleDateFormat.format(now);

        type.setUpdateTime(updateTime);

        Response response = new Response();

        int statusNum = typeService.updateType(type);

        if (statusNum == 1){
            response.setStatus(200);
            response.setData(statusNum);
            response.setMsg("修改类型成功");
        }

        return response;
    }

    @PutMapping("/del")
    public Response delType(
            @RequestParam(value = "typeId", required = true) String typeId
    ){

        Response response = new Response();

        int statusNum = typeService.delType(typeId);

        response.setData(statusNum);
        if (statusNum == 1){
            response.setStatus(200);
            response.setMsg("执行删除命令成功");
        }else {
            response.setStatus(400);
            response.setMsg("执行删除命令成功，但没有此typeId");
        }

        return response;
    }
}
